package com.wangkang.eduservice.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wangkang.eduservice.entity.EduSubject;
import com.wangkang.eduservice.entity.excel.SubjectData;
import com.wangkang.eduservice.enums.EduResultCode;
import com.wangkang.eduservice.service.EduSubjectService;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;


@AllArgsConstructor
@NoArgsConstructor
//在contorller层(controller层会调用service层)中先进行文件上传，再在listener层中进行文件中数据的读取与保存。
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    /**
     *
     *  因为SubjectExcelListener不能交给Spring管理，需要自己new，不能注入其它对象
     *  不能实现数据库操作
     *  需要手动注入
     *  即不能@Autowired实现自动装配注入
     */

    private EduSubjectService subjectService;


    /**
     * 读取excel内容，一行一行进行读取
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {

        System.out.println("-----------"+ subjectData);
        subjectData = Optional.ofNullable(subjectData).orElseThrow(() -> GuliException.from(EduResultCode.FILE_IS_EMPTY));


        //添加一级科目,不存在，则添加
        EduSubject oneSubject = getOneSubjectByName(subjectService, subjectData.getOneSubjectName());
        if(Objects.isNull(oneSubject)){
            oneSubject  = EduSubject.builder()
                    .parentId("0")
                    .title(subjectData.getOneSubjectName())
                    .build();

            subjectService.save(oneSubject);
        }

        //获取一级科目id
        String parentId = oneSubject.getId();

        EduSubject twoSubject = getTwoSubjectByName(subjectService, subjectData.getTwoSubjectName(), parentId);

        //如果二级科目不存在，则保存
        if(Objects.isNull(twoSubject)){
            twoSubject  = EduSubject.builder()
                    .parentId(parentId)
                    .title(subjectData.getTwoSubjectName())
                    .build();

            subjectService.save(twoSubject);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }


    /**
     *  根据名称查询一级科目是否存在
     * @param subjectService
     * @param name  科目名称
     * @return  Subject  科目
     */
    private EduSubject getOneSubjectByName(EduSubjectService subjectService,String name){
        LambdaQueryWrapper<EduSubject> query = new LambdaQueryWrapper<>();
        query.eq(EduSubject::getTitle,name);
        query.eq(EduSubject::getParentId,"0");
        return subjectService.getOne(query);
    }


    /**
     *  根据名称与二级分类的parentId来查询二级科目是否存在
     *  若二级分类的名称已存在，但与新加的二级分类所对应的一级分类不同的话，也查不到
     *  查不到的二级分类在invoke方法中能被新增进数据库中的edu_subject表格中。
     * @param subjectService
     * @param name  科目名称
     * @param pid 一级科目id
     * @return  Subject  科目
     */
    private EduSubject getTwoSubjectByName(EduSubjectService subjectService,String name,String pid){
        LambdaQueryWrapper<EduSubject> query = new LambdaQueryWrapper<>();
        query.eq(EduSubject::getTitle,name);
        query.eq(EduSubject::getParentId,pid);
        return subjectService.getOne(query);
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {

        System.out.println("head"+headMap);
    }

}
